<template>
  <div class="login-container">
    <div class="mode-toggle">
      <el-switch
        v-model="isDark"
        inline-prompt
        :active-icon="Sunny"
        :inactive-icon="Moon"
        @change="toggleDark"
      />
    </div>
    <div class="login-bg">
      <div class="login-content">
        <div class="login-card">
          <div class="login-header">
            <h2 class="login-title">Hi！欢迎登录</h2>
            <h3 class="login-subtitle">Vue3+Element Plus后台管理系统</h3>
            <el-form :model="loginForm" :rules="rules" ref="ruleForm">
              <!-- 用户名 -->
              <div class="form-group" @keyup.enter="handleLogin">
                <div class="form-control">
                  <el-form-item prop="username">
                    <el-input
                        v-model="loginForm.username"
                        placeholder="请输入您的用户名"
                        class="validateInput">
                      <i slot="prefix"
                         class="el-icon-user focus-icon input-icon-login">
                      </i>
                    </el-input>
                  </el-form-item>
                </div>
              </div>

              <!-- 密码 -->
              <div class="form-group" @keyup.enter="handleLogin">
                <div class="form-control">
                  <el-form-item prop="unlockPassword">
                    <el-input
                        v-model="loginForm.unlockPassword"
                        placeholder="请输入您的密码"
                        type="password"
                        class="validateInput">
                      <i slot="prefix"
                         class="el-icon-lock focus-icon input-icon-login">
                      </i>
                    </el-input>
                  </el-form-item>
                </div>
              </div>
              <el-button
                  :loading="loading"
                  :loading-icon="Eleme"
                  type="primary"
                  class="login-buttion"
                  @click="handleLogin"
              >
                <!--                {{ $t('login.logIn') }}-->
                登录
              </el-button>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style>
@import 'normalize.css';
</style>

<style scoped>
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: var(--el-bg-color-page);
  margin: 0;
  padding: 0;
  color: var(--el-text-color-primary);
  background-color: var(--el-bg-color);
}

.mode-toggle {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 2;
}

.login-bg {
  position: absolute;
  width: 100%;
  height: 100%;
}

.login-content {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.login-card {
  padding: 40px;
  background: var(--el-bg-color-overlay);
  border-radius: 12px;
  box-shadow: var(--el-box-shadow-dark);
  transition: all 0.3s ease;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  color: var(--el-text-color-primary);
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 10px;
}

.login-subtitle {
  color: var(--el-text-color-secondary);
  font-size: 16px;
  font-weight: 400;
}

.validateInput {
  transition: all 0.3s ease;
}

.validateInput:hover {
  transform: translateY(-2px);
}

.login-buttion {
  width: 100%;
  margin-top: 20px;
  height: 45px;
  font-size: 16px;
  letter-spacing: 2px;
  transition: all 0.3s ease;
}

.login-buttion:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
</style>

<script setup lang="ts">
import { ref } from 'vue'
import { useDark, useToggle } from '@vueuse/core'
import type { FormInstance } from 'element-plus'
import { Eleme, Sunny, Moon } from '@element-plus/icons-vue'

const isDark = useDark()
const toggleDark = useToggle(isDark)

const ruleForm = ref<FormInstance>()
const loading = ref(false)

const loginForm = ref({
  username: "",
  password: "",
  unlockPassword: ""
})

const rules = {
  username: [
    {required: true, message: "用户名不能为空!", trigger: "blur"}
  ],
  unlockPassword: [
    {required: true, message: "密码不能为空!", trigger: "blur"}
  ],
  validatedCode: [
    {required: true, message: "验证码不能为空!", trigger: "blur"}
  ]
}

const handleLogin = () => {
  ruleForm.value?.validate(valid => {
    if (valid) {
      loginSubmit()
    } else {
      return false
    }
  })
}

const loginSubmit = (data = {}) => {
  loading.value = true
  setTimeout(() => {
    loading.value = false
  }, 1000)
}
</script>
